import store from '@/store'

module.exports = (params) => {
	var usermsg = uni.getStorageSync('usermsg') ? JSON.parse(uni.getStorageSync('usermsg')):{}
	let url = params.url;
	let method = params.method;
	let data = params.data || {};
	let header = params.header || {
		'accept':'application/json',
		'Content-Type':'application/json',
		'x-user': usermsg.userid||'',
		'x-language': 'ch',
		'x-corp': '',
		'x-domain': usermsg.domain ||'',
		'x-token': usermsg.sessionid ||'',
		// 'authorization': uni.getStorageSync('user_token')
	}
	//	发起请求 加载动画
	// if (params.url != 'user/login') {
	// 	if (!params.hideLoading) {
	// 		uni.showLoading({
	// 			title: "加载中"
	// 		})
	// 	}
	// }
	if (params.url == 'user/login') {
		header = {}
	}
	//	发起网络请求
	uni.request({
		url: url,
		method: method || "GET",
		header: header,
		data: data,
		dataType: "json",
		contentType: 'application/json,charset=UTF-8',
		sslVerify: false, //	是否验证ssl证书
		success: res => {
			console.log(res)
			if(res.statusCode !='200'){
				typeof params.fail == "function" && params.fail(res);
				if(res.statusCode=='0'){
					uni.showModal({
						title: '提示',
						cancelText:'取消',
						confirmText:'确定',
						content:  '网络异常(错误码' + res.statusCode + ')' + ';url:'+url
					})
					return
				}else if(res.statusCode=='500'){
					uni.showModal({
						title: '提示',
						cancelText:'取消',
						confirmText:'确定',
						content:  '服务器异常(错误码' + res.statusCode + ')' + ';url:'+url
					})
					return
				}else if(res.statusCode=='401'){
						uni.showModal({
							title: '提示',
							content: '该账号在其他地方登录',
							showCancel: false,
							success: function (res) {
								_this.appUtil.logout()
							}
						});
						return
				}else{
					uni.showModal({
						title: '提示',
						cancelText:'取消',
						confirmText:'确定',
						content:  '客户端异常(错误码' + res.statusCode + ')' + ';url:'+url
					})
					return
				}
			}
			typeof params.success == "function" && params.success(res.data);
		},
		fail: err => {
			console.log(err)
			uni.showModal({
				title: '提示',
				cancelText:'取消',
				confirmText:'确定',
				content: err.msg || '网络异常请稍后再试' + ';url:'+url
			})
			typeof params.fail == "function" && params.fail(err);
		},
		complete: (e) => {
			console.log("请求完成");
			// uni.hideLoading()
			typeof params.complete == "function" && params.complete(e.data);
			return;
		}
	})
}
